//
//  CQStatusView.swift
//  weibo
//
//  Created by chengqiang on 16/7/24.
//  Copyright © 2016年 chengqiangBiaodashi. All rights reserved.
//

import UIKit

class CQStatusView: UIView {

    
    var status:CQstatus?{
        didSet{
            if let profile_image_url = status?.user?.profile_image_url{
                let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            
            nameLable.text=status?.user?.screen_name
            timeLable.text=NSDate.date(status?.created_at ?? "未知时间")?.sinaDateDescription()
            sourceLable.text=status?.source
            memberImageView.image=status?.user?.mbrabkImage
            verifedImageView.image=status?.user?.verifiedimage
            contentlable.text=status?.text
            pictureView.imageUrl=status?.storepicutureURLs
            
            
            pictureView.snp_updateConstraints { (make) in
                var margin:CGFloat = 0
                if status?.storepicutureURLs != nil && status?.storepicutureURLs?.count>0{
                    margin=CellMargin
                }
                make.top.equalTo(contentlable.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor=UIColor.whiteColor()
        setupUI()
        
    }
    
    private func setupUI(){
        
        self.addSubview(iconView)
        self.addSubview(nameLable)
        self.addSubview(timeLable)
        self.addSubview(sourceLable)
        self.addSubview(memberImageView)
        self.addSubview(verifedImageView)
        self.addSubview(contentlable)
        self.addSubview(pictureView)
        
        
        iconView.snp_makeConstraints { (make) in
            make.leading.top.equalTo(CellMargin)
            make.size.equalTo(CGSize(width: CellIconWH, height: CellIconWH))
        }
        
        nameLable.snp_makeConstraints { (make) in
            make.leading.equalTo(iconView.snp_trailing).offset(CellMargin)
            make.top.equalTo(iconView)
        }
        
        timeLable.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLable)
            make.bottom.equalTo(iconView)
        }
        
        sourceLable.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLable.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(timeLable)
        }
        
        memberImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLable.snp_trailing).offset(CellMargin)
            make.centerY.equalTo(nameLable)
        }
        
        verifedImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        contentlable.snp_makeConstraints { (make) in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
        }
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(contentlable.snp_bottom).offset(CellMargin)
            
            make.size.equalTo(CGSize(width: 359, height: 359))
        }
        
        self.snp_makeConstraints { (make) in
            make.bottom.equalTo(pictureView).offset(CellMargin)
        }
        
    }   
    //用户头像
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named:"avatar"))
    //用户名称
    private lazy var nameLable:UILabel = UILabel(textColor:UIColor.darkGrayColor(),fontSize: 15)
    //时间Lable
    private lazy var timeLable:UILabel = UILabel(textColor: UIColor.orangeColor())
    //消息来源
    private lazy var sourceLable:UILabel = UILabel(textColor: UIColor.lightGrayColor())    //会员等级
    private lazy var memberImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //认证图标
    private lazy var  verifedImageView:UIImageView = UIImageView(image: UIImage(named:"avatar_vip"))
    
    //微博文本内容
    private lazy var contentlable:UILabel = {
       let lable = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 16)
        
        lable.numberOfLines=0
        
        lable.preferredMaxLayoutWidth = UIScreen.width() - 2*CellMargin
        return lable
    }()
    
    //配图试图
    private lazy var pictureView:CQStatusPictureView = CQStatusPictureView()
    
}
